package com.ymatou.iapi.anlimember.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.anlimember.enums.OpenTypeEnum;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AnlihuiCallService;
import com.ymttest.database.model.anlihui.MemberRelation;
import com.ymttest.database.sqlwapper.anlihui.InviteRelationWrapper;
import com.ymttest.database.sqlwapper.anlihui.MemberRelationWrapper;

public class Ts_AutoUpgrade {
	
	private static MemberRelationWrapper memberRelationWrapper = new MemberRelationWrapper();
	private static InviteRelationWrapper inviteRelationWrapper = new InviteRelationWrapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("自动升级");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_001() {
		Logger.start(true, "安利会员邀新，用户允许手动升级");
		try {
			
			Long memberId = 53221L;
			Long inviteeId = 314487L;
			
			// 将会员等级更新成2，且openType为1
			memberRelationWrapper.updateMemberOpenType(memberId, false, 2, 1);
			
			//将邀请关系购买普通商品时间置为null
			inviteRelationWrapper.updateBuyProductTime(inviteeId, memberId);
			
			// 发送发货消息，并自动更新openType
			AnlihuiCallService.onOrderShipped(inviteeId);
			
			Thread.sleep(3000);
			
			MemberRelation memberRelation = memberRelationWrapper.selectByMemberId(53221L);
			
			Logger.verifyEquals(
					(byte) OpenTypeEnum.UPGRADE_FROM_ANLI_INVITE_NEW_USER
							.getCode(), memberRelation.getOpen_type(),
					"验证Open_type");
			Logger.verifyEquals(true, memberRelation.getUpgradeable(),
					"验证Upgradeable");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_002() {
		Logger.start(true, "安利会员邀新会员，用户允许手动升级");
		try {
			
			Long memberId = 53222L;
			Long inviteeId = 3324421L;
			
			// 将会员等级更新成2，且openType为1
			memberRelationWrapper.updateMemberOpenType(memberId, false, 2, 1);
			
			//将邀请关系购买会员时间置为null
			inviteRelationWrapper.updateBuyVipTime(inviteeId, memberId);
			
			// 发送确认收货消息，并自动更新openType
			AnlihuiCallService.onMemberOrderConfirmed(inviteeId);
			
			MemberRelation memberRelation = memberRelationWrapper.selectByMemberId(memberId);
			
			Logger.verifyEquals(
					(byte) OpenTypeEnum.UPGRADE_FROM_ANLI_INVITE_NEW_MEMBER
							.getCode(), memberRelation.getOpen_type(),
					"验证Open_type");
			Logger.verifyEquals(true, memberRelation.getUpgradeable(),
					"验证Upgradeable");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindInviteRelation_003() {
		Logger.start(true, "普通用户邀新，用户自动成为海淘会员");
		try {
			
			Long memberId = 53221L;
			Long inviteeId = 314487L;
			
			// 删除会员
			memberRelationWrapper.deleteByMemberId(memberId);
			
			//将邀请关系购买会员时间置为null
			inviteRelationWrapper.updateBuyProductTime(inviteeId, memberId);
			
			// 发送确认收货消息
			AnlihuiCallService.onOrderShipped(inviteeId);
			
			MemberRelation memberRelation = memberRelationWrapper.selectByMemberId(memberId);
			
			Logger.verifyEquals(
					(byte) OpenTypeEnum.UPGRADE_FROM_NORMAL
							.getCode(), memberRelation.getOpen_type(),
					"验证Open_type");
			Logger.verifyEquals(false, memberRelation.getUpgradeable(),
					"验证Upgradeable");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
